﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Framework.Common
{
    public static class PathHelper
    {
        #region private
        private static string _rootPath = AppDomain.CurrentDomain.BaseDirectory.Replace('/', Path.DirectorySeparatorChar);
        #endregion

        #region 兼容c/s和b/s的MapPath
        /// <summary>
        /// 兼容c/s和b/s的MapPath
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MapPath(string path)
        {
            if (HttpContext.Current != null)
                return HttpContext.Current.Server.MapPath(path);
            else
                return PhysicalPath(path.Replace('/', Path.DirectorySeparatorChar).Replace("~", ""));
        }
        #endregion

        #region c/s程序中问文件的路径
        /// <summary>
        /// 获取c/s文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string PhysicalPath(string path)
        {
            return string.Concat(_rootPath.TrimEnd(Path.DirectorySeparatorChar), Path.DirectorySeparatorChar, path.TrimStart(Path.DirectorySeparatorChar));
        }
        #endregion

        #region 不出错的MapPath
        /// <summary>
        /// 不出错的MapPath
        /// </summary>
        /// <param name="path"></param>
        /// <param name="endwithDirectorySeparator"></param>
        /// <returns></returns>
        public static string CalculatePath(string path, bool endwithDirectorySeparator = false)
        {
            string calculateLocation = null;

            // 如果是本地路径
            if ((path.IndexOf(":\\") != -1) || (path.IndexOf("\\\\") != -1))
                calculateLocation = path;
            else
                calculateLocation = MapPath(path);

            if (endwithDirectorySeparator)
            {
                // 以斜杠结尾
                if (!calculateLocation.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    calculateLocation += Path.DirectorySeparatorChar;
            }
            return calculateLocation;
        }
        #endregion

        #region 获取host http://domain.com:81
        /// <summary>
        /// 获取host http://domain.com:81
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string HostPath(Uri uri)
        {
            string str = (uri.Port == 80) ? string.Empty : (":" + uri.Port.ToString());
            return string.Format("{0}://{1}{2}", uri.Scheme, uri.Host, str);
        }
        #endregion

        #region 补齐 http://domain.com
        /// <summary>
        /// 补齐 http://domain.com
        /// </summary>
        /// <param name="local"></param>
        /// <returns></returns>
        public static string FullPath(string local)
        {
            if (string.IsNullOrEmpty(local))
            {
                return local;
            }
            if (local.ToLower().StartsWith("http://") || local.ToLower().StartsWith("https://"))
            {
                return local;
            }
            if (HttpContext.Current == null)
            {
                return local;
            }
            return string.Concat(HostPath(HttpContext.Current.Request.Url), local);
        }
        #endregion

        #region 返回 目录 和文件名 [0] 目录 [1]文件名
        /// <summary>
        /// 返回 目录 和文件名 [0] 目录 [1]文件名
        /// </summary>
        /// <param name="fullPath">完整路径</param>
        /// <returns></returns>
        public static string[] GetDirPathAndFileName(string fullPath)
        {
            return new string[] { Path.GetDirectoryName(fullPath), Path.GetFileName(fullPath) };
        }
        #endregion
    }
}
